package com.basic.business.service.impl;

import com.basic.business.model.entity.SysUser;
import com.basic.business.model.dto.SysUserDto;
import com.basic.business.model.vo.SysUserVo;
import com.basic.business.mapper.SysUserMapper;
import com.basic.business.service.SysUserService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.*;

import java.util.*;

import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;

/**
 * (SysUser)表服务实现类
 *
 * @author huage
 * @since 2021-07-21 00:05:40
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysUserVo findById(Integer id) {
        SysUser sysUser = this.sysUserMapper.selectById(id);

        //将SysUser转成SysUserVo
        SysUserVo sysUserVo = null;
        if (sysUser != null) {
            sysUserVo = new SysUserVo();
            BeanUtils.copyProperties(sysUser, sysUserVo);
        }

        return sysUserVo;
    }

    /**
     * 查询多条数据
     *
     * @param sysUserDto 实例对象
     * @param pageNum    页数
     * @param pageSize   每页条数
     * @return 对象列表
     */
    @Override
    public PageInfo<SysUserVo> findPageList(SysUserDto sysUserDto, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        //将SysUser转成SysUserVo
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDto, sysUser);
        List<SysUser> sysUserList = this.sysUserMapper.selectList(sysUser);

        PageInfo<SysUserVo> pageInfo = new PageInfo<>();
        BeanUtils.copyProperties(new PageInfo<>(sysUserList), pageInfo);
        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param sysUserDto 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserVo add(SysUserDto sysUserDto) {
        //将SysUserDto转成SysUser
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDto, sysUser);
        this.sysUserMapper.insert(sysUser);

        return this.findById(sysUser.getId());
    }

    /**
     * 修改数据
     *
     * @param sysUserDto 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserVo modify(SysUserDto sysUserDto) {
        //将SysUserDto转成SysUser
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDto, sysUser);
        this.sysUserMapper.update(sysUser);

        return this.findById(sysUser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean remove(Integer id) {
        return this.sysUserMapper.deleteById(id) > 0;
    }
}

